﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
using System.IO;
namespace QuanLyDeTaiKHCN.UserControls.Manager
{
    public partial class TheoDoiHopDong : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                if (_db.tblPheDuyetDeTais.Where(r => r.MaDeTai.Trim() == madetai).Count() > 0)
                {
                    BindGrid(madetai);
                    pncontent.Visible = true;
                    lblMessage.Visible = false;
                    lblMessage.Text = "";
                    BindData();
                    TongKinhPhi();
                }
                else
                {
                    pncontent.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Đề tài này chưa phê duyệt cho phép thực hiện.";
                }
            }
        }

        protected void LinkAddFile_Click(object sender, EventArgs e)
        {
            if (filebienban.HasFile)
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                DateTime t = DateTime.Now;
                string filename = (t.Day < 10 ? "0" + t.Day.ToString() : t.Day.ToString()) + (t.Month < 10 ? "0" + t.Month.ToString() : t.Month.ToString()) + t.Year.ToString() + t.Hour.ToString() + t.Minute.ToString() + t.Second.ToString() + Path.GetExtension(filebienban.FileName);
                tblFile _tb = new tblFile();
                _tb.MaDeTaiDuAn = madetai;
                _tb.FileThuyetMinh = filename;
                _tb.GhiChuFileThuyetMinh = txtghichufilethuyetminh.Text;
                _tb.NgayNhanThuyetMinh = CatalogAccess.ConvertToDate(txtNgayNhan.Text);
                _tb.FileCuaBuoc = 6;
                try
                {
                    _db.tblFiles.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    BindGrid(madetai);
                    filebienban.SaveAs(Server.MapPath(PageURL.local + "filehoso/" + filename));
                    txtghichufilethuyetminh.Text = "";
                }
                catch { }
            }
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            var item = _db.tblFiles.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblFiles.DeleteOnSubmit(item);
                _db.SubmitChanges();
                try
                {
                    string filename = ((Label)grid.Rows[e.RowIndex].FindControl("lblFileName")).Text;
                    System.IO.File.Delete(Server.MapPath(PageURL.local + "filehoso/" + filename));
                }
                catch { }
                BindGrid(madetai);
            }
            catch { }
        }

        protected void BindGrid(string madetai)
        {
            var list = from p in _db.tblFiles.Where(r => r.MaDeTaiDuAn.Trim() == madetai && r.FileCuaBuoc == 6)
                       select new
                       {
                           ID = p.ID,
                           TrichYeu = p.GhiChuFileThuyetMinh.Trim(),
                           NgayLuu = string.Format("{0:dd/MM/yyyy}", p.NgayNhanThuyetMinh),
                           FileThuyetMinh = p.FileThuyetMinh.Trim(),
                           //sau khi up len host thi xai link1:
                           //Fileonline =domain_name + "\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //test xaif link2
                           //Fileonline = "localhost\\" +directory+"\\"+ p.FileThuyetMinh.Trim()
                           //demo link3:
                           Fileonline = "http://thongkekhcnbd.khcnbinhduong.gov.vn/userfiles/vanban/9475707965_493_296.pdf"

                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            try
            {
                var _tb = _db.tblTheoDoiHopDongs.Single(r => r.MaDeTai.Trim() == madetai);
                _tb.SoHopDong = txtSoHopDong.Text;
                if (CatalogAccess.ConvertToDateBool(txtNgayHopDong.Text))
                {
                    _tb.NgayHopDong = CatalogAccess.ConvertToDate(txtNgayHopDong.Text);
                }
                _tb.NoiDung = txtGhiChuHopDong.Text;
                if (CatalogAccess.ConvertToDateBool(txtThoiGianThucHienTu.Text))
                {
                    _tb.ThucHienTu = CatalogAccess.ConvertToDate(txtThoiGianThucHienTu.Text);
                }
                if (CatalogAccess.ConvertToDateBool(txtThoiGianThucHienDen.Text))
                {
                    _tb.ThucHienDen = CatalogAccess.ConvertToDate(txtThoiGianThucHienDen.Text);
                }
                string str1 = "";
                string str2 = "";
                //giai doan thuc hien
                str2 += txtTuNgayGD1.Text + "!";
                str2 += txtDenNgayGD1.Text + "!";
                str2 += txtKinhPhiGD1.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD2.Text + "!";
                str2 += txtDenNgayGD2.Text + "!";
                str2 += txtKinhPhiGD2.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD3.Text + "!";
                str2 += txtDenNgayGD3.Text + "!";
                str2 += txtKinhPhiGD3.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD4.Text + "!";
                str2 += txtDenNgayGD4.Text + "!";
                str2 += txtKinhPhiGD4.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD5.Text + "!";
                str2 += txtDenNgayGD5.Text + "!";
                str2 += txtKinhPhiGD5.Text + "!";
                str1 += str2 + "@";
                _tb.GiaiDoanThucHien = str1;
                //phu luc hop dong
                str1 = "";
                str2 = "";
                str2 += txtPhuLucHopDongSo1.Text + "!";
                str2 += txtNgayPhuLuc1.Text + "!";
                str2 += txtNoiDungHD1.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtPhuLucHopDongSo2.Text + "!";
                str2 += txtNgayPhuLuc2.Text + "!";
                str2 += txtNoiDungHD2.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtPhuLucHopDongSo3.Text + "!";
                str2 += txtNgayPhuLuc3.Text + "!";
                str2 += txtNoiDungHD3.Text + "!";
                str1 += str2 + "@";
                _tb.PhuLucHopDong = str1;
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu";
                }
                catch { lblstatus.Text = "Không thêm được."; }
            }
            catch //truong hop them moi
            {
                tblTheoDoiHopDong _tb = new tblTheoDoiHopDong();
                _tb.MaDeTai = madetai;
                _tb.SoHopDong = txtSoHopDong.Text;
                if(CatalogAccess.ConvertToDateBool(txtNgayHopDong.Text))
                {
                    _tb.NgayHopDong = CatalogAccess.ConvertToDate(txtNgayHopDong.Text);
                }
                _tb.NoiDung = txtGhiChuHopDong.Text;
                if (CatalogAccess.ConvertToDateBool(txtThoiGianThucHienTu.Text))
                {
                    _tb.ThucHienTu = CatalogAccess.ConvertToDate(txtThoiGianThucHienTu.Text);
                }
                if (CatalogAccess.ConvertToDateBool(txtThoiGianThucHienDen.Text))
                {
                    _tb.ThucHienDen= CatalogAccess.ConvertToDate(txtThoiGianThucHienDen.Text);
                }
                string str1 = "";
                string str2 = "";
                //giai doan thuc hien
                str2 += txtTuNgayGD1.Text + "!";
                str2 += txtDenNgayGD1.Text + "!";
                str2 += txtKinhPhiGD1.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD2.Text + "!";
                str2 += txtDenNgayGD2.Text + "!";
                str2 += txtKinhPhiGD2.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD3.Text + "!";
                str2 += txtDenNgayGD3.Text + "!";
                str2 += txtKinhPhiGD3.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD4.Text + "!";
                str2 += txtDenNgayGD4.Text + "!";
                str2 += txtKinhPhiGD4.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtTuNgayGD5.Text + "!";
                str2 += txtDenNgayGD5.Text + "!";
                str2 += txtKinhPhiGD5.Text + "!";
                str1 += str2 + "@";
                _tb.GiaiDoanThucHien = str1;
                //phu luc hop dong
                str1 = "";
                str2 = "";
                str2 += txtPhuLucHopDongSo1.Text + "!";
                str2 += txtNgayPhuLuc1.Text + "!";
                str2 += txtNoiDungHD1.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtPhuLucHopDongSo2.Text + "!";
                str2 += txtNgayPhuLuc2.Text + "!";
                str2 += txtNoiDungHD2.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                str2 += txtPhuLucHopDongSo3.Text + "!";
                str2 += txtNgayPhuLuc3.Text + "!";
                str2 += txtNoiDungHD3.Text + "!";
                str1 += str2 + "@";
                str2 = "";
                _tb.PhuLucHopDong = str1;
                try
                {
                    _db.tblTheoDoiHopDongs.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu";
                }
                catch { lblstatus.Text = "Không thêm được."; }
            }
        }
        protected void BindData()
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            if (_db.tblTheoDoiHopDongs.Where(r => r.MaDeTai.Trim() == madetai).Count() != 0)
            {
                var item = _db.tblTheoDoiHopDongs.Single(r => r.MaDeTai.Trim() == madetai);
                try
                {
                    txtSoHopDong.Text = item.SoHopDong.Trim();
                }
                catch { txtSoHopDong.Text = ""; }
                txtNgayHopDong.Text = string.Format("{0:dd/MM/yyyy}", item.NgayHopDong);
                try { txtGhiChuHopDong.Text = item.NoiDung.Trim(); }
                catch { txtGhiChuHopDong.Text = ""; }
                txtThoiGianThucHienTu.Text = string.Format("{0:dd/MM/yyyy}", item.ThucHienTu);
                txtThoiGianThucHienDen.Text = string.Format("{0:dd/MM/yyyy}", item.ThucHienDen);
                //giai doan thuc hien kinh phi
                string[] sogiaidoan = item.GiaiDoanThucHien.Split('@');
                string[] gd;
                gd = sogiaidoan[0].Split('!');
                try
                {
                    txtTuNgayGD1.Text = gd[0];
                }
                catch { }
                try
                {
                    txtDenNgayGD1.Text = gd[1];
                }
                catch { }
                try
                {
                    txtKinhPhiGD1.Text = gd[2];
                }
                catch { }
                gd = sogiaidoan[1].Split('!');
                try
                {
                    txtTuNgayGD2.Text = gd[0];
                }
                catch { }
                try
                {
                    txtDenNgayGD2.Text = gd[1];
                }
                catch { }
                try
                {
                    txtKinhPhiGD2.Text = gd[2];
                }
                catch { }
                gd = sogiaidoan[2].Split('!');
                try
                {
                    txtTuNgayGD3.Text = gd[0];
                }
                catch { }
                try
                {
                    txtDenNgayGD3.Text = gd[1];
                }
                catch { }
                try
                {
                    txtKinhPhiGD3.Text = gd[2];
                }
                catch { }
                gd = sogiaidoan[3].Split('!');
                try
                {
                    txtTuNgayGD4.Text = gd[0];
                }
                catch { }
                try
                {
                    txtDenNgayGD4.Text = gd[1];
                }
                catch { }
                try
                {
                    txtKinhPhiGD4.Text = gd[2];
                }
                catch { }
                gd = sogiaidoan[4].Split('!');
                try
                {
                    txtTuNgayGD5.Text = gd[0];
                }
                catch { }
                try
                {
                    txtDenNgayGD5.Text = gd[1];
                }
                catch { }
                try
                {
                    txtKinhPhiGD5.Text = gd[2];
                }
                catch { }
                //phu luc hop dong
                string[] phuluc = item.PhuLucHopDong.Split('@');
                string[] pl;
                pl = phuluc[0].Split('!');
                try
                {
                    txtPhuLucHopDongSo1.Text = pl[0];
                }
                catch { }
                try
                {
                    txtNgayPhuLuc1.Text = pl[1];
                }
                catch { }
                try
                {
                    txtNoiDungHD1.Text = pl[2];
                }
                catch { }
                pl = phuluc[1].Split('!');
                try
                {
                    txtPhuLucHopDongSo2.Text = pl[0];
                }
                catch { }
                try
                {
                    txtNgayPhuLuc2.Text = pl[1];
                }
                catch { }
                try
                {
                    txtNoiDungHD2.Text = pl[2];
                }
                catch { }
                pl = phuluc[2].Split('!');
                try
                {
                    txtPhuLucHopDongSo3.Text = pl[0];
                }
                catch { }
                try
                {
                    txtNgayPhuLuc3.Text = pl[1];
                }
                catch { }
                try
                {
                    txtNoiDungHD3.Text = pl[2];
                }
                catch { }
            }
        }

        protected void txtKinhPhiGD1_TextChanged(object sender, EventArgs e)
        {
            TongKinhPhi();
        }

        private void TongKinhPhi()
        {
            int kp = 0;
            int tongkp = 0;
            try
            {
                if (txtKinhPhiGD1.Text != "")
                {
                    kp = int.Parse(txtKinhPhiGD1.Text);
                }
            }
            catch { }
            tongkp += kp;
            kp = 0;
            try
            {
                if (txtKinhPhiGD2.Text != "")
                {
                    kp = int.Parse(txtKinhPhiGD2.Text);
                }
            }
            catch { }
            tongkp += kp;
            kp = 0;
            try
            {
                if (txtKinhPhiGD3.Text != "")
                {
                    kp = int.Parse(txtKinhPhiGD3.Text);
                }
            }
            catch { }
            tongkp += kp;
            kp = 0;
            try
            {
                if (txtKinhPhiGD4.Text != "")
                {
                    kp = int.Parse(txtKinhPhiGD4.Text);
                }
            }
            catch { }
            tongkp += kp;
            kp = 0;
            try
            {
                if (txtKinhPhiGD5.Text != "")
                {
                    kp = int.Parse(txtKinhPhiGD5.Text);
                }
            }
            catch { }
            tongkp += kp;
            txtTongKinhPhi.Text = string.Format("{0:#,##}", tongkp);
        }

        protected void txtKinhPhiGD2_TextChanged(object sender, EventArgs e)
        {
            TongKinhPhi();
        }

        protected void txtKinhPhiGD3_TextChanged(object sender, EventArgs e)
        {
            TongKinhPhi();
        }

        protected void txtKinhPhiGD4_TextChanged(object sender, EventArgs e)
        {
            TongKinhPhi();
        }

        protected void txtKinhPhiGD5_TextChanged(object sender, EventArgs e)
        {
            TongKinhPhi();
        }
    }
}